package com.gitee.l0km.codegen.decorator;

import java.beans.PropertyDescriptor;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gitee.l0km.codegen.annotations.AnnotationRuntimeException;
import com.gitee.l0km.codegen.base.NewSourceInfoAbstract;
import com.google.common.base.Throwables;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.gitee.l0km.com4j.basex.bean.jdk.BeanPropertySupportImpl.BEAN_SUPPORT;
import static com.google.common.base.Preconditions.checkArgument;

/**
 * enum support 生成器{@code NewSourceInfo}实现
 * @author guyadong
 *
 * @param <T>
 */
public class EnumSupport<T> extends NewSourceInfoAbstract<T> {
	private static final Logger logger = LoggerFactory.getLogger(EnumSupport.class);
	EnumMetadata<?> enumMetadata;
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EnumSupport(Class<? extends T> baseClass) {
		super(null, null, baseClass);
		enumMetadata = new EnumMetadata(baseClass.getSimpleName(),baseClass);
	}
	@Override
	protected Map<String, PropertyDescriptor> readProperties() {
		return BEAN_SUPPORT.getProperties(this.refClass, 0, true);
	}
	@Override
	public boolean compile() {
		boolean compileOk=false;
		try{
			super.compile();
			compileOk = true;
		} catch(AnnotationRuntimeException e){
			logger.error(e.toString());
		} 
		return compileOk;
	}
	@Override
	protected void checkClass(Class<T> interfaceClass, Class<? extends T> refClass, Class<? extends T> baseClass) {
		checkNotNull(baseClass,"baseClass is null");
		checkArgument(baseClass.isEnum(),"{} is not a enum type",baseClass.getName());
	}

	@Override
	protected void createMethodsNeedGenerated() {
	}
	
	public EnumMetadata<?> getEnumMetadata() {
		return enumMetadata;
	}
	/**
	 * @return 返回枚举类型的所有对象
	 */
	public Object getEnums(){
		try {
			return baseClass.getMethod("values").invoke(null);
		} catch (Exception e) {
			Throwables.throwIfUnchecked(e);
			throw new RuntimeException(e);
		}
	}
}
